package com.cryptape.cita_wallet.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.CloseUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.cryptape.cita_wallet.CytonApplication;
import com.cryptape.cita_wallet.item.Wallet;
import com.cryptape.cita_wallet.util.Blockies;
import com.cryptape.cita_wallet.util.db.DBWalletUtil;
import com.mangoworld.android.R;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import de.hdodenhof.circleimageview.CircleImageView;

public class MyInvitationCodeActivity extends NBaseActivity {

    private CircleImageView walletPhoto;
    private FrameLayout cardView;
    private TextView yourMidTv;
    private ImageView qrCodeIv;

    private Bitmap mBitmap;
    private String mid;

    @Override
    protected int getContentLayout() {
        return R.layout.activity_my_invitation_code;
    }

    @Override
    protected void initView() {
        cardView = findViewById(R.id.cardView);
        yourMidTv = findViewById(R.id.yourMidTv);
        qrCodeIv = findViewById(R.id.qrCodeIv);
        walletPhoto = findViewById(R.id.wallet_photo);
//        ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) cardView.getLayoutParams();
//        layoutParams.topMargin = walletPhoto.getTop() + SizeUtils.getMeasuredHeight(walletPhoto) / 2;
//        cardView.setLayoutParams(layoutParams);
    }

    @Override
    protected void initData() {
        Wallet wallet = DBWalletUtil.getCurrentWallet(MyInvitationCodeActivity.this);
        mid = CytonApplication.getInstance().MIDCode;
        mBitmap = CodeUtils.createImage(mid, SizeUtils.getMeasuredWidth(qrCodeIv), SizeUtils.getMeasuredHeight(qrCodeIv), null);//BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)
        qrCodeIv.setImageBitmap(mBitmap);
        yourMidTv.setText(mid);
        walletPhoto.setImageBitmap(Blockies.createIcon(wallet.address));
//        TokenService.findMid().subscribe(new Subscriber<String>() {
//            @Override
//            public void onCompleted() {
//                dismissProgressBar();
//            }
//
//            @Override
//            public void onError(Throwable e) {
//                dismissProgressBar();
//                Toast.makeText(MyInvitationCodeActivity.this, getString(R.string.network_error), Toast.LENGTH_SHORT).show();
//            }
//
//            @Override
//            public void onNext(String s) {
//                MyMIDBean midBean = new Gson().fromJson(s, MyMIDBean.class);
//                if (midBean.getCode() == 0) {
//                    MyMIDBean.DataBean dataBean = midBean.getData();
//                    if (dataBean != null) {
//                        mid = dataBean.getLnvitationCode();
//                        mBitmap = CodeUtils.createImage(mid, SizeUtils.px2dp(200), SizeUtils.px2dp(200), null);//BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)
//                        qrCodeIv.setImageBitmap(mBitmap);
//                        yourMidTv.setText(mid);
//                    }
//                } else {
//
//                }
//            }
//        });
    }

    @Override
    protected void initAction() {
        yourMidTv.setOnClickListener(v -> {
            ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            ClipData mClipData = ClipData.newPlainText("mid", yourMidTv.getText());
            if (cm != null) {
                cm.setPrimaryClip(mClipData);
                Toast.makeText(mActivity, R.string.copy_success, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        CloseUtils.closeIO();
    }
}
